<?php
session_start();
require_once("../models/Unit.php");
require_once("../models/Payment.php");
class ViewAccountController{
	var $requests;
	function getAccountForm($unit_name){
		$unit = new Unit;
		$unit->connect();
		$accountInfo = $unit->getAccountInformation($unit_name);
		?>
		<form id="editaccountform5">
		Lesse Name: <input value="<?php echo $accountInfo['name']; ?>" id="name" name="name" /><br />
		Home address: <input value="<?php echo $accountInfo['home']; ?>" id="home_address" name="home_address" /><br />
		Student Number: <input value="<?php echo $accountInfo['studnum']; ?>" id="studnum" name="studnum" /><br />
		Cellphone Number: <input value="<?php echo $accountInfo['cellnum']; ?>" id="cellnum" name="cellnum" /><br />
		Course: <input value="<?php echo $accountInfo['course']; ?>" id="course" name="course" /><br />
		College: <input value="<?php echo $accountInfo['college']; ?>" id="college" name="college" /><br />
		Classification: <input value="<?php echo $accountInfo['classification']; ?>" id="classification" name="classification" /><br />
		Parent's Name: <input value="<?php echo $accountInfo['parent']; ?>" id="parents_name" name="parents_name" /><br />
		Parent's Contact: <input value="<?php echo $accountInfo['contact']; ?>" id="contact_nums" name="contact_nums" /><br />
		Other Occupants:<br /> <textarea id="other_occupants" name="other_occupants"><?php echo $accountInfo['occupants']; ?></textarea><br />
		<input type="submit" value="Update" onclick="if($('#editaccountform5').valid()){updateAccountInfo()}" />
		</form>
		<?php
	}
	
	function getAccount($unit_name){
		$unit = new Unit;
		$unit->connect();
		$accountInfo = $unit->getAccountInformation($unit_name);
		?>
		
		Lesse Name: <?php echo $accountInfo['name']; ?><br />
		Home address: <?php echo $accountInfo['home']; ?><br />
		Student Number: <?php echo $accountInfo['studnum']; ?><br />
		Cellphone Number: <?php echo $accountInfo['cellnum']; ?><br />
		Course: <?php echo $accountInfo['course']; ?><br />
		College: <?php echo $accountInfo['college']; ?><br />
		Classification: <?php echo $accountInfo['classification']; ?><br />
		Parent's Name: <?php echo $accountInfo['parent']; ?><br />
		Parent's Contact: <?php echo $accountInfo['contact']; ?><br />
		Other Occupants:<br /> <?php echo $accountInfo['occupants']; ?><br />
		<input type="button" value="Edit" onclick="viewAccountForm()" />
		
		<br />
		<br />
		

		Payments made:<br />

		<table id="paymentstable" class="tablesorter">
		<thead>
			<tr>
				<th>Date of Payment</th>
				<th>Mode of Payment</th>
				<th>Receipt No.</th>
				<th>Amount</th>
			</tr>
		</thead>
		<tbody>
		<?php
		$total = 0;
		$i=0;
		$payment=new Payment;
		$row=$payment->getPayments($unit_name);
		while($row[$i]!=null){
		?>	<tr>
				<td><?php echo $row[$i]['date_received']?></td>
				<td><?php echo $row[$i]['mode']?></td>
				<td><?php echo $row[$i]['receipt_no']?></td>
				<td class="amount"><?php echo $row[$i]['amount']?></td>
			</tr>
		<?php
			$total = $total + $row[$i]['amount'];
			$i++;
		}
		?>
		</tbody>
			<tr>
				<td colspan="3">Total Payments</td>
				<td class="amount">P<?php echo $total?></td>
			</tr>
		</table>
		
		<?php
		$query1 = "SELECT monthly_rent from unit where unit_name='$unit_name'";
		$result1 = mysql_query($query1)
		or die(mysql_error());  
		$row1 = mysql_fetch_array($result1);
		
		
		$query = "SELECT month(start_date) m, year(start_date) y, day(start_date) d, start_date , deposit from unit where unit_name='$unit_name'";
		$result = mysql_query($query)
		or die(mysql_error());  
		$row = mysql_fetch_array($result);
		$year =  $row['y'];
		$month = $row['m'];
		$day = $row['d'];
		$deposit = $row['deposit'];

	//	$year = 2009;
	//	$month = 9;
	//	$day = 9;
		
		$year2 = date("Y");
		$month2 = date("m");
		$day2 = date("d");
		
		$start = mktime(0,0,0,$month,$day,$year);
		$now = mktime(0,0,0,$month2,$day2,$year2);
		
		
		

		
		echo "Start Date: ".date("Y-m-d",$start)."<br />";
		echo "Financial Information as of ".date("Y-m-d",$now)."<br />";
		$compare_now = mktime(0,0,0,$month2-1,$day2+1,$year2);
		
		
		$a=$month;
		$c=$day;
		
		if($month<10)
			$a="0".$a;
			
		if($c<10)
			$c="0".$c;
		$month++;
		$d1=cal_days_in_month(CAL_GREGORIAN,date("m",$start),date("Y",$start));
		$total2= round(($d1-$day+1)/$d1*$row1['monthly_rent'],2);
		$totalwithpenalty=$total2;
		$balance=0;
		?>
		<table id="invoices" class="tablesorter">
			<thead>
				<tr>
					<th>Type</th>
					<th>Date</th>
					<th>Description or Receipt No.</th>
					<th>Amount</th>
					<th>Balance</th>
				</tr>
			</thead>
			<tbody>
			<tr>
				<td>Bill</td>
				<td><?php echo date("Y-m-$c",$start)?></td>
				<td><?php 
					if($total2!=$row1['monthly_rent']){
						echo ($d1+1-$day) ." days for the month of ".date("M Y",$start);
					}
					else{
						echo "Rent for ".date('M Y',$start);
					}
				
				?></td>
				<td class="amount"><?php echo $total2?></td><?php $balance = $total2?>
				<td class="amount"><?php echo $balance ?></td>
			</tr>
				<?php 
				$b = date("Y-m-17",$start);
				$penaltydate = $start;
				
				$query3 = "SELECT * from payment where unit = '$unit_name' and date_received between '".date("Y-m-01",$start)."' and '". date("Y-m-17",$start)."' order by date_received";
				$result3 = mysql_query($query3)
				or die(mysql_error());
				while($row3 = mysql_fetch_array($result3)){
					$amount = -$row3['amount'];
					$receipt_no = $row3['receipt_no'];
					$balance = $balance+$amount;
					?>
					<tr>
						<td>Payment</td>
						<td><?php echo $row3['date_received']?></td>
						<td><?php echo $receipt_no?></td>
						<td class="amount"><?php echo $amount?></td>
						<td class="amount"><?php echo $balance?></td>
					</tr>
					<?php

				}
				
				
				
				
				
				$query2 = "SELECT sum(amount) paid from payment where unit='$unit_name' and date_received<='$b'";
				//echo $query2;
				$result2 = mysql_query($query2)
				or die(mysql_error());  
				$row2 = mysql_fetch_array($result2);
				
				if($totalwithpenalty-$row2['paid']>$deposit){
					$amount = min($row1['monthly_rent'],$totalwithpenalty-$row2['paid'])*.1;
					$balance = $balance + $amount;
					?>
					<tr>
						<td>Penalty</td>
						<td><?php echo date("Y-m-18",$penaltydate)?></td>
						<td>10% of <?php echo $amount*10?></td>
						<td class="amount"><?php echo $amount?></td>
						<td class="amount"><?php echo $balance?></td>
					</tr>
					<?php
					$totalwithpenalty = $totalwithpenalty + $amount;
				}
				
				
				
				
				
				
				
				$query3 = "SELECT * from payment where unit = '$unit_name' and date_received between '".date("Y-m-18",$start)."' and '". date("Y-m-$d1",$start)."' order by date_received";
				$result3 = mysql_query($query3)
				or die(mysql_error());
				while($row3 = mysql_fetch_array($result3)){
					$amount = -$row3['amount'];
					$receipt_no = $row3['receipt_no'];
					$balance = $balance+$amount;
					?>
					<tr>
						<td>Payment</td>
						<td><?php echo $row3['date_received']?></td>
						<td><?php echo $receipt_no?></td>
						<td class="amount"><?php echo $amount?></td>
						<td class="amount"><?php echo $balance?></td>
					</tr>
					<?php

				}

		$start = mktime(0,0,0,$month++,1,$year);
		while($start<$compare_now){
			?>
			<tr>
			<?php
			$d1=cal_days_in_month(CAL_GREGORIAN,date("m",$start),date("Y",$start));
			$amount = $row1['monthly_rent'];
			?>
			<td>Bill</td>
			<td><?php echo date("Y-m-01",$start)?></td>
			<td><?php echo "Rent for ".date('M Y',$start);?></td>
			<td class="amount"><?php echo $amount?></td>
			<?php
			

			
			$a = date("Y-m-17",$start);
			
			
			$penaltydate = $start;
			$query3 = "SELECT * from payment where unit = '$unit_name' and date_received between '".date("Y-m-01",$start)."' and '". date("Y-m-17",$start)."' order by date_received";
			$result3 = mysql_query($query3)
			or die(mysql_error());

			$query4 = "SELECT * from payment where unit = '$unit_name' and date_received between '".date("Y-m-18",$start)."' and '". date("Y-m-$d1",$start)."' order by date_received";
			$result4 = mysql_query($query4)
			or die(mysql_error());
			
			$start = mktime(0,0,0,$month++,1,$year);
			$total2 += $amount;
			$totalwithpenalty += $amount;
			
			$query2 = "SELECT sum(amount) paid from payment where unit='$unit_name' and date_received<='$a'";
			$result2 = mysql_query($query2)
			or die(mysql_error());  
			$row2 = mysql_fetch_array($result2);
			$balance=$balance+$amount;
			?>
			<td class="amount"><?php echo $balance?></td>
			</tr>
			<?php
			
			while($row3 = mysql_fetch_array($result3)){
				$amount = $row3['amount']*-1;
				$balance = round($balance + $amount);
					?>
					<tr>
						<td>Payment</td>
						<td><?php echo $row3['date_received']?></td>
						<td><?php echo $row3['receipt_no']?></td>
						<td class="amount"><?php echo $amount?></td>
						<td class="amount"><?php echo $balance?></td>
					</tr>
					<?php
			}
			
			
			if($totalwithpenalty-$row2['paid']>$deposit){
				$amount = min($row1['monthly_rent'],$totalwithpenalty-$row2['paid'])*.1;
				$balance = $balance + $amount;
				?>
				<tr>
					<td>Penalty</td>
					<td><?php echo date("Y-m-18",$penaltydate)?></td>
					<td>10% of <?php echo $amount*10?></td>
					<td class="amount"><?php echo $amount?></td>
					<td class="amount"><?php echo $balance?></td>
				</tr>
				<?php
				$totalwithpenalty = $totalwithpenalty + $amount;
			}
			

			while($row4 = mysql_fetch_array($result4)){
				$amount = $row4['amount']*-1;
				$balance = round($balance + $amount);
					?>
					<tr>
						<td>Payment</td>
						<td><?php echo $row4['date_received']?></td>
						<td><?php echo $row4['receipt_no']?></td>
						<td class="amount"><?php echo $amount?></td>
						<td class="amount"><?php echo $balance?></td>
					</tr>
					<?php
			}
			
			
		}
			if(date("m")==date("m",$start)){
			?>
			<tr>
			<?php
				$amount =  $row1['monthly_rent'];
				$balance  = $balance + $amount;
				$d1=cal_days_in_month(CAL_GREGORIAN,date("m",$start),date("Y",$start));
				?>

				<td>Bill</td>
				<td><?php echo date("Y-m-01",$start)?></td>
				<td><?php echo "Rent for ".date('M Y',$start);?></td>
				<td class="amount"><?php echo $amount?></td>
				<td class="amount"><?php echo $balance?></td>
				<?php
			
			$a = date("Y-m-17",$start);
			$penaltydate = $start;
			
			$query3 = "SELECT * from payment where unit = '$unit_name' and date_received between '".date("Y-m-01",$penaltydate)."' and '". date("Y-m-17",$penaltydate)."' order by date_received";
			$result3 = mysql_query($query3)
			or die(mysql_error());
			
			while($row3 = mysql_fetch_array($result3)){
				$amount = $row3['amount']*-1;
				$balance = round($balance + $amount);
					?>
					<tr>
						<td>Payment</td>
						<td><?php echo $row3['date_received']?></td>
						<td><?php echo $row3['receipt_no']?></td>
						<td class="amount"><?php echo $amount?></td>
						<td class="amount"><?php echo $balance?></td>
					</tr>
					<?php
			}
			
			
			
			$start = mktime(0,0,0,$month++,1,$year);
			$total2 += $row1['monthly_rent'];
			$totalwithpenalty += $row1['monthly_rent'];
			$outstanding = $total2-$total;
			$query2 = "SELECT sum(amount) paid from payment where unit='$unit_name' and date_received<='$a'";
			$result2 = mysql_query($query2)
			or die(mysql_error());  
			$row2 = mysql_fetch_array($result2);

			if($totalwithpenalty-$row2['paid']>$deposit&&date("d")>17){
				$amount = min($row1['monthly_rent'],$totalwithpenalty-$row2['paid'])*.1;
				$balance = $balance + $amount;
				?>
				<tr>
					<td>Penalty</td>
					<td><?php echo date("Y-m-18",$penaltydate)?></td>
					<td>10% of <?php echo $amount*10?></td>
					<td class="amount"><?php echo $amount?></td>
					<td class="amount"><?php echo $balance?></td>
				</tr>
				<?php
				$totalwithpenalty = $totalwithpenalty + $amount;
			}
			
			$query3 = "SELECT * from payment where unit = '$unit_name' and date_received between '".date("Y-m-18",$penaltydate)."' and '". date("Y-m-$d1",$penaltydate)."' order by date_received";
			$result3 = mysql_query($query3)
			or die(mysql_error());
			
			while($row3 = mysql_fetch_array($result3)){
				$amount = $row3['amount']*-1;
				$balance = round($balance + $amount);
					?>
					<tr>
						<td>Payment</td>
						<td><?php echo $row3['date_received']?></td>
						<td><?php echo $row3['receipt_no']?></td>
						<td class="amount"><?php echo $amount?></td>
						<td class="amount"><?php echo $balance?></td>
					</tr>
					<?php
			}
			
			
			
			
			 }?>
			</tbody>
			<tr>
				<td colspan="4">Outstanding Balance</td>
				<td class="amount">P<?php echo $balance?></td>
			</tr>
		</table>
		
	
		
		
		
		
		<div id="stopcontract">
		<?php if(($_SESSION['username'])=="admin"){?>
			<a href="#" onclick="stopContract2();" >Stop Contract</a>
			<div id="stopContractDialog" title="Stop Contract">
			</div>
		<?php }?>
		</div>
		<?php
	}
	
	function requestRequest($id){
		$request = new Request;
		$request->connect();
		$requestMessage = $request->getRequest($id);
		echo $requestMessage['message'];
	}

}

	$a = new ViewAccountController;
	$unit = new Unit;
	$unit->connect();
	if(!$unit->isOccupied($_REQUEST['unit_name'])){
		?>
		<div id="sendContractForm">
			Unit is not occupied.<br />
			<a href="#" onclick="viewStartContractForm()">Start Contract</a>
		</div>
		
		
		<?php
		
		die;
	}
	if($_REQUEST['mode']==1)
		$a->getAccountForm($_REQUEST['unit_name']);
	else
		$a->getAccount($_REQUEST['unit_name']);
	
?>